﻿using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using UnityEngine;

namespace HurricanePackage
{
   public  static class TextureHelper
    {
        /// <summary>
        /// sprite 转换到byte[]
        /// </summary>
        public static byte[] GetByte(Sprite _sp)
        {
            if (_sp == null)
                return null;

            Texture2D temp = _sp.texture;

            return temp.EncodeToPNG();

        }
        /// <summary>
        /// 获取sprite
        /// </summary>
        /// <param name="_ca"></param>
        /// <returns></returns>
        public static Sprite GetSprite(WebCamTexture _ca)
        {
            return GetSprite(GetPhotoPixel(_ca), _ca.requestedWidth, _ca.requestedHeight);
        }


        /// <summary>
        /// WebCamTexture 转成 byte[]
        /// </summary>
        /// <param name="_ca"></param>
        /// <returns></returns>
        public static byte[] GetPhotoPixel(WebCamTexture _ca)
        {
            Texture2D texture = new Texture2D(_ca.width, _ca.height);
            int y = 0;
            while (y < texture.height)
            {
                int x = 0;
                while (x < texture.width)
                {
                    UnityEngine.Color color = _ca.GetPixel(x, y);
                    texture.SetPixel(x, y, color);
                    ++x;
                }
                ++y;
            }
            texture.Apply();
            //        texture.name = name ;
            byte[] pngData = GetJpgData(texture);

            return pngData;
        }
        /// <summary>
        /// 控制照片大小
        /// </summary>
        /// <param name="_te"></param>
        /// <returns></returns>
        public static byte[] GetJpgData(Texture2D _te)
        {
            byte[] data = null;
            int quelity = 75;
            while (quelity > 20)
            {
                data = _te.EncodeToJPG(quelity);
                int size = data.Length / 1024;
                if (size > 30)
                {
                    quelity -= 5;
                }
                else
                {
                    break;
                }
            }

            return data;
        }
        /// <summary>
        /// byte转sprite
        /// </summary>
        /// <param name="_byte"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Sprite GetSprite(byte[] _byte, int width, int height)//生成的尺寸
        {
            Texture2D texture = new Texture2D(width, height);
            texture.LoadImage(_byte);

            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            return sprite;
        }

        /// <summary>
        /// 保存图片到本地
        /// </summary>
        public static void SaveImgLocally(Texture2D _texture, string _path)
        {
            if(_texture == null || string.IsNullOrEmpty(_path))
                return;
            byte[] bytes = _texture.EncodeToJPG();
            if(bytes == null)
                return;

            File.WriteAllBytes(_path,bytes);
        }

    }
}
